package org.example.code.test2024.eight_mouth;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class test0715 {
    int []a={0,0,1,-1};
    int []b={1,-1,0,0};
    public void solve(char[][] board) {
        int n = board.length;
        int m = board[0].length;
        boolean [][]map = new boolean[n][m];
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                map[i][j]=false;
            }
        }
        for(int i=0;i<n;i++){
            if(board[i][0]=='O'&&!map[i][0]){
                map[i][0]=true;
                dfs(board,map,i,0);
            }
            if(board[i][m-1]=='O'&&!map[i][m-1]){
                map[i][m-1]=true;
                dfs(board,map,i,m-1);
            }
        }
        for(int i=0;i<m;i++){
            if(board[0][i]=='O'&&!map[0][i]){
                map[0][i]=true;
                dfs(board,map,0,i);
            }
            if(board[n-1][i]=='O'&&!map[n-1][i]){
                map[n-1][i]=true;
                dfs(board,map,n-1,i);
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(map[i][j]){
                    board[i][j]='O';
                }else{
                    board[i][j]='X';
                }
                System.out.println(board[i][j]+" ");
            }
            System.out.println();
        }
    }

    private void dfs(char[][] board, boolean[][] map, int x, int y) {
        int n = board.length;
        int m = board[0].length;
        for(int i=0;i<4;i++){
            int xx = x+a[i];
            int yy = y+b[i];
            if(xx>-1&&xx<n&&yy>-1&&yy<m){
                if(board[xx][yy]=='O'&&!map[xx][yy]){
                    map[xx][yy]=true;
                    dfs(board,map,xx,yy);
                }
            }
        }
    }

//    private HashMap<Node,Node> visit = new HashMap<>();
//    public Node cloneGraph(Node node) {
//        if(node == null){
//            return node;
//        }
//
//        if(visit.containsKey(node)){
//            return visit.get(node);
//        }
//
//        Node clone = new Node(node.val,new ArrayList<>());
//        visit.put(node,clone);
//
//        for (Node n:node.neighbors) {
//            clone.neighbors.add(n);
//        }
//        return clone;
//    }

    public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
        int n = Integer.MAX_VALUE;
        double []result = new double[equations.size()];
        Double [][]num = new Double[n][2];
        Double max = 0.0;
        for(int i=0;i<equations.size();i++){
            List<String> list = equations.get(i);
            int x = Integer.parseInt(list.get(0));
            int y = Integer.parseInt(list.get(1));
            num[x][y] = values[i];
            num[y][x] = 1/values[i];
            max = Math.max(values[i],max);
        }
        for(int i=0;i<queries.size();i++){
            List<String> list = queries.get(i);
            int x = Integer.parseInt(list.get(0));
            int y = Integer.parseInt(list.get(1));
            result[i] = num[x][y];
        }
        return result;
    }

    List<List<Integer>> list;
    int[] visited;//计算每个节点的入度
    Queue<Integer> queue;

    public boolean canFinish(int numCourses, int[][] prerequisites) {
        list = new ArrayList<List<Integer>>();
       visited = new int[numCourses];
       for(int i=0;i<numCourses;++i){
           list.add(new ArrayList<Integer>());
       }
        for (int[] info:prerequisites) {
            list.get(info[1]).add(info[0]);
            visited[info[0]]++;
        }
        queue = new LinkedList<Integer>();
        for(int i=0;i<numCourses;++i){//将所有入度为0的节点入队
            if (visited[i]==0) {
                queue.offer(i);
            }
        }
        while(!queue.isEmpty()){
            int x = queue.poll();//出队
            List<Integer> integers = list.get(x);
            for (int a:integers) {
                --visited[a];
                if(visited[a]==0){
                    queue.offer(a);
                }
            }
        }
        for(int i=0;i<numCourses;i++){
            if(visited[i]!=0){
                return false;
            }
        }
        return true;
    }

    public int[] findOrder(int numCourses, int[][] prerequisites) {
        int[] result = new int[numCourses];
        list = new ArrayList<List<Integer>>();
        visited = new int[numCourses];
        for(int i=0;i<numCourses;++i){
            list.add(new ArrayList<Integer>());
        }
        for (int[] info:prerequisites) {
            list.get(info[1]).add(info[0]);
            visited[info[0]]++;
        }
        queue = new LinkedList<Integer>();
        for(int i=0;i<numCourses;++i){//将所有入度为0的节点入队
            if (visited[i]==0) {
                queue.offer(i);
            }
        }
        int k = 0;
        while(!queue.isEmpty()){
            int x = queue.poll();//出队
            result[k]=x;
            for (int a:list.get(x)) {
                --visited[a];
                if(visited[a]==0){
                    queue.offer(a);
                }
            }
            k++;
        }
        if(k==numCourses){
            return null;
        }
        return result;
    }

    //-10,-3,0,5,9
    public TreeNode sortedArrayToBST(int[] nums) {
        return fun(nums,0,nums.length-1);
    }

    private TreeNode fun(int[] nums, int left, int right) {
        int mid = (left+right)/2;
        TreeNode node = new TreeNode(nums[mid]);
        node.left = fun(nums,0,mid);
        node.right = fun(nums,mid+1,right);
        return node;
    }

    public static String solution(String arg1) throws FileNotFoundException {
        String bestMatch = null;
        String[] split = arg1.split(",");
        String name = split[0];
        String sex = split[1];
        String birth = split[2];
        List<person> list = new ArrayList<>();
        try {
            BufferedReader bufferedReader =
                    new BufferedReader(new FileReader("src/resu/final_user_list.txt"));
            String str = bufferedReader.readLine();
            while(str!=null){
                String[] split1 = str.split(",");
                if(split1[1].equals(sex)){
                    list.add(new person(split1[0],split1[1],split1[2]));
                }
                str = bufferedReader.readLine();
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        person per = new person();
        int bir = Integer.MAX_VALUE;
        int na = Integer.MAX_VALUE;
        for (person p:list) {
            int x = birth.compareTo(p.getBirth());
            if(Math.abs(x)<bir){
                per = p;
                bir = x;
            }else {
                char[] charArray = p.getName().toCharArray();
                char[] charArray1 = name.toCharArray();

            }
        }
        bestMatch = per.getName()+","+per.getSex()+","+per.getBirth();
        return bestMatch;
    }


    public static void main(String[] args) throws FileNotFoundException {
        String str = "yxy,male,2002-02-02";
        System.out.println(solution(str));
    }


    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> result = new ArrayList<>();
        if(k==1){
            ArrayList<Integer> list = new ArrayList<>();
            list.add(n);
            result.add(list);
            return result;
        }
        return result;
    }

}

class person{
    private String name;
    private String sex;
    private String birth;

    public person() {
    }

    public person(String name, String sex, String birth) {
        this.name = name;
        this.sex = sex;
        this.birth = birth;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getBirth() {
        return birth;
    }

    public void setBirth(String birth) {
        this.birth = birth;
    }
}